package com.example.demo.util;

import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import javax.servlet.annotation.WebServlet;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

public class QrCodeUtil {

    private static Integer IMAGE_WITH = 100;
    private static Integer IMGAE_HEIGHT = 100;

    private static String imagePath = "E:\\ImagePicture\\QQ图片20240529192626.jpg";

    public static void encode(OutputStream stream) throws Exception {

        BufferedImage encode = createEncode();
        ImageIO.write(encode,"png",stream);
    }

    public static BufferedImage createEncode()throws Exception
    {
        HashMap hashMap = new HashMap<>();
        hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hashMap.put(EncodeHintType.CHARACTER_SET,"UTF-8");
        hashMap.put(EncodeHintType.MARGIN,1);

        BitMatrix bitMatrix = new MultiFormatWriter().encode("cc", BarcodeFormat.QR_CODE,300,300,hashMap);

        int width = bitMatrix.getWidth();
        int height = bitMatrix.getHeight();

        BufferedImage bufferedImage = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);

        for (int i=0;i<width;i++)
        {
            for (int j=0;j<height;j++)
            {
                bufferedImage.setRGB(i,j,bitMatrix.get(i,j)?0xFF000000:0xFFFFFFFF);
            }
        }
        insertImage(bufferedImage,imagePath);
        return bufferedImage;
    }

    public static void insertImage(BufferedImage source,String imagePath) throws Exception
    {

        File file = new File(imagePath);
        if(!file.exists())
        {
            return;
        }
        Image image = ImageIO.read(file);
        int height = image.getHeight(null);
        int width = image.getWidth(null);

        if(true)
        {
            if(width>IMAGE_WITH)
            {
                width = 100;
            }
            if(height>IMGAE_HEIGHT)
            {
                height = 100;
            }



            Image src = image.getScaledInstance(width,height,Image.SCALE_SMOOTH);
            BufferedImage tag = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

            Graphics graphics = tag.getGraphics();

            graphics.drawImage(src,0,0,null);

            graphics.dispose();

            image =src;
        }


        Graphics2D graphics2D = source.createGraphics();
        int x = (300-width)/2;
        int y = (300-height)/2;

        graphics2D.drawImage(image,x,y,width,height,null);

        Shape shape = new RoundRectangle2D.Float(x, y, width, height, 6, 6);
        //
        graphics2D.setStroke(new BasicStroke(3f));

        graphics2D.draw(shape);
        graphics2D.dispose();
    }






    public static void main(String[] args) throws WriterException, IOException {
        String qrCodeText = "这里是二维码内的文本";
        String imagePath = "path/to/your/image.jpg";
        BufferedImage image = ImageIO.read(new File(imagePath));

        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, image.getWidth(), image.getHeight());


        // 将二维码嵌入图片
        BufferedImage newImage = insertQRCode(image, bitMatrix);

        // 保存新图片
        String outputPath = "path/to/output/image.png";
        MatrixToImageWriter.writeToFile(bitMatrix, "PNG", new File(outputPath));
    }

    private static BufferedImage insertQRCode(BufferedImage originalImage, BitMatrix qrCode) {
        int qrCodeWidth = qrCode.getWidth();
        int qrCodeHeight = qrCode.getHeight();
        // 计算嵌入位置
        int x = originalImage.getWidth() - qrCodeWidth;
        int y = originalImage.getHeight() - qrCodeHeight;

        // 将二维码图像转换为RGB_TRANSPARENT
        ///BufferedImage qrCodeImage = qrCode.toImage();

        // 将二维码图像嵌入原始图像
        for (int i = 0; i < qrCodeWidth; i++) {
            for (int j = 0; j < qrCodeHeight; j++) {
                if (qrCode.get(i, j)) {
                    // 不改变原始图片的颜色
                    //originalImage.setRGB(x + i, y + j, qrCodeImage.getRGB(i, j));
                }
            }
        }
        return originalImage;
    }

}
